package com.lq.mytool.strategy;

import com.lq.mytool.enums.AttendancePunchWayTypeEnum;
import com.lq.mytool.utils.SpringApplicationUtil;

import java.util.Currency;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author Nydia
 * @menu
 * @since 2022/2/1
 */
public class PunchWayFactory {

    private PunchWayFactory(){}

    private static final Map<String,PunchWayStrategy> PUNCH_WAY_STRATEGY_CACHE = new HashMap<>();

    /**
     * 获取当前事物所处理的方式
     */
    public static PunchWayStrategy getPunchWayStrategy(AttendancePunchWayTypeEnum punchWayType){
       return PunchWayFactory.getPunchWayStrategy(punchWayType.name());
    }

    private static PunchWayStrategy getPunchWayStrategy(String punchWayName) {
        if (PUNCH_WAY_STRATEGY_CACHE.isEmpty()){
            init();
        }
        return PUNCH_WAY_STRATEGY_CACHE.get(punchWayName);
    }

    private static void init() {
        synchronized (PUNCH_WAY_STRATEGY_CACHE) {
            // 双重校验
            if (PUNCH_WAY_STRATEGY_CACHE.isEmpty()) {
                Map<String, PunchWayStrategy> beansMapOfType = SpringApplicationUtil.getBeansMapOfType(PunchWayStrategy.class);
                beansMapOfType.entrySet().stream().
                        forEach(entry -> PUNCH_WAY_STRATEGY_CACHE.put(entry.getValue().getPunchWayType().name(), entry.getValue()));
            }
        }
    }
}
